package org.wlf.websocket;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.NotificationCompat;

import java.util.ArrayList;
/*****************************************************
 * author:      wz
 * email:       wangzhong0116@foxmail.com
 * version:     1.0
 * date:        2016/11/9 09:54
 * description:
 *****************************************************/
@SuppressLint("NewApi")
public class NotifyUtil {

    private int NOTIFICATION_ID;
    private NotificationManager nm;
    private Notification notification;
    private NotificationCompat.Builder cBuilder;
    private Context mContext;


    public NotifyUtil(Context context, int ID) {
        this.NOTIFICATION_ID = ID;
        mContext = context;
        nm = (NotificationManager) mContext
                .getSystemService(Activity.NOTIFICATION_SERVICE);
        cBuilder = new NotificationCompat.Builder(mContext);
    }


    private void setCompatBuilder(PendingIntent pendingIntent, int smallIcon, String ticker,
                                  String title, String content, boolean sound, boolean vibrate, boolean lights) {

        cBuilder.setContentIntent(pendingIntent);
        cBuilder.setSmallIcon(smallIcon);
        cBuilder.setTicker(ticker);
        cBuilder.setContentTitle(title);
        cBuilder.setContentText(content);
        cBuilder.setWhen(System.currentTimeMillis());

		//设置滑动小时
        cBuilder.setAutoCancel(true);

        cBuilder.setPriority(NotificationCompat.PRIORITY_MAX);

        int defaults = 0;

        if (sound) {
            defaults |= Notification.DEFAULT_SOUND;
        }
        if (vibrate) {
            defaults |= Notification.DEFAULT_VIBRATE;
        }
        if (lights) {
            defaults |= Notification.DEFAULT_LIGHTS;
        }

        cBuilder.setDefaults(defaults);
    }


    /**
     * 显示消息通知
     * @param pendingIntent
     * @param smallIcon
     * @param ticker
     * @param title
     * @param content
     */
    public void notify_mailbox(PendingIntent pendingIntent, int smallIcon, int largeIcon, ArrayList<String> messageList,
                               String ticker, String title, String content, boolean sound, boolean vibrate, boolean lights) {

        setCompatBuilder(pendingIntent, smallIcon, ticker, title, content, sound, vibrate, lights);
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), largeIcon);

        //卧槽，设置大图标居然不管用，经过查询，发现是部分锤子手机不行，I fu le you;
        cBuilder.setLargeIcon(bitmap);
        cBuilder.setSmallIcon(smallIcon);
        //只能用小图了
        cBuilder.setDefaults(Notification.DEFAULT_ALL);
        cBuilder.setAutoCancel(true);

        NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
        for (String msg : messageList) {
            inboxStyle.addLine(msg);
        }
        inboxStyle.setSummaryText("[" + messageList.size() + "条]" + title);
        cBuilder.setStyle(inboxStyle);
        sent();
    }


    private void sent() {
        notification = cBuilder.build();
        nm.notify(NOTIFICATION_ID, notification);
    }


    public void clear() {
        nm.cancelAll();
    }
}
